//
//  HYTradeView.m
//  HaiYue
//
//  Created by skyline on 2018/4/20.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYTradeView.h"
CGFloat const SEGHEIGHT = 40;
@interface HYTradeView()<UIScrollViewDelegate>
{
    CGFloat startOffsetX;
}
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) HYSeg *seg;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, assign) BOOL didEndDecelerating;
@end
@implementation HYTradeView
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.scrollView.hidden = NO;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (UIView *)line
{
    if (!_line)
    {
        _line = [[UIView alloc] init];
        _line.backgroundColor = [UIColor whiteColor];
        [self addSubview:_line];
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.height.mas_offset(SEGHEIGHT);
        }];
    }
    return _line;
}

- (HYSeg *)seg
{
    if (!_seg)
    {
        _seg = [[HYSeg alloc] initWithItems:@[@"已购买",@"出售",@"收藏",@"提现"]];
        _seg.titleColor = TEXT_GRAY_COLOR;
        _seg.selectedtTitleColor = THEME_COLOR;
        _seg.bounds = CGRectMake(0, 0, SCREEN_WIDTH - 120, SEGHEIGHT);
        [_seg addTarget:self action:@selector(actionForSegCurrent) forControlEvents:UIControlEventValueChanged];
        [self.line addSubview:_seg];
        [_seg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(0, 60, 0, 60));
        }];
    }
    return _seg;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.contentSize = CGSizeMake(self.seg.items.count * SCREEN_WIDTH, SCREEN_HEIGHT - NAVIBAR_HEIGHT - STATUS_HEIGHT - TABBAR_HEIGHT - SEGHEIGHT);
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_scrollView];
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.top.equalTo(self.line.mas_bottom);
            make.bottom.equalTo(self);
        }];
        
        
        for (int i = 0; i < self.seg.items.count; i++)
        {
           HYLabel *label = [[HYLabel alloc] initWithText:[NSString stringWithFormat:@"第%d个",i+1] textColor:THEME_COLOR];
            [_scrollView addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self);
                make.centerX.equalTo(self.scrollView).with.offset(SCREEN_MIN_LENGTH * i);
                make.size.mas_offset(label.bounds.size);
            }];
        }
    }
    return _scrollView;
}

#pragma mark - HYSeg method
- (void)actionForSegCurrent
{
    NSLog(@"currentIndex : %d  previousIndex  : %d",self.seg.currentIndex, self.seg.previousIndex);
    [self.scrollView setContentOffset:CGPointMake(self.seg.currentIndex * SCREEN_MIN_LENGTH, 0) animated:YES];
}
#pragma mark - UIScrollViewDelegate 事件
// 开始减速的时候开始self.didEndDecelerating = NO;结束减速就会置为YES,如果滑动很快就还是NO。
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    self.didEndDecelerating = NO;
}
//
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.didEndDecelerating = YES;
    self.seg.currentIndex = scrollView.contentOffset.x / SCREEN_MIN_LENGTH;
}

// 再次拖拽的时候，判断有没有因为滑动太快而没有调用结束减速的方法。
// 如果没有，四舍五入手动确定位置。这样就可以解决滑动过快的问题
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (!self.didEndDecelerating)
    {
        // 先计算当期的page/index
        self.seg.currentIndex = scrollView.contentOffset.x / SCREEN_MIN_LENGTH;
        //再四舍五入推算本该减速时的scrollView的contentOffset。即：roundf(index)*self.screenWidth]
    }
}

 // called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
